﻿
using Sys;

using System.Silverlight;
using System.Silverlight.Presentation;
using System.Silverlight.Presentation.Animation;
using seh = Sys.EventHandler;
using sea = Sys.EventArgs;
using System;

namespace SBT
{
    public enum ToggleButtonListBehavior
    {
        RadioButtonList =1,
        CheckboxButtonList=2
    }
    public class ToggleButtonList : AgComponent
    {
        private seh m_onItemPushedHandler;
        private seh m_onItemUnPushedHandler;

        private ArrayList m_items;
        public ArrayList Items
        {
            get { return m_items; }
            set 
            {
                if (m_items != value)
                {
                    ArrayList old = m_items;
                    m_items = value;                   
                    RaisePropertyChanged("items");
                    if (IsInitialized)
                    {
                        InitItems(old, value);
                    }
                }
            }
        }

        private ArrayList m_selectedItems;
        public ArrayList SelectedItems
        {
            get { return m_selectedItems; }
        }

        public void add_selectionChanged(seh handler)
        {
            this.Events.AddHandler("selectionChanged", handler);
        }
        public void remove_selectionChanged(seh handler)
        {
            Events.RemoveHandler("selectionChanged", handler);
        }
        private ToggleButtonListBehavior m_behavior;
        public ToggleButtonListBehavior Behavior
        {
            get
            {
                return m_behavior;
            }
            set
            {
                if (m_behavior != value)
                {
                    m_behavior = value;
                    RaisePropertyChanged("behavior");
                }
            }
        }

        public ToggleButtonList()
        {
            m_selectedItems = new ArrayList();
            m_behavior = ToggleButtonListBehavior.RadioButtonList;
        }

        public override void Initialize()
        {
            base.Initialize();
            m_onItemPushedHandler = OnItemPushed;
            m_onItemUnPushedHandler = OnItemUnPushed;

            InitItems(null, Items);
        }
        private bool m_isRadioPushing = false;
        private void OnItemPushed(object sender, sea args)
        {
            if (Behavior == ToggleButtonListBehavior.RadioButtonList)
            {
                m_isRadioPushing = true;
                ArrayList copy = ArrayList.Clone(m_selectedItems);
                for (int i = 0; i < copy.Length; i++)
                {
                    if(copy[i] != sender)
                        ((ToggleButton)copy[i]).IsPushed = false;
                }
                m_isRadioPushing = false;
            }
            ArrayList.Add(m_selectedItems, sender);
            RaiseEvent("selectionChanged");
        }
        private void OnItemUnPushed(object sender, sea args)
        {
            
            ArrayList.Remove(m_selectedItems, sender);
            if (!m_isRadioPushing)
            {
                RaiseEvent("selectionChanged");
            }
            
        }

        private void InitItems(ArrayList oldItems, ArrayList newItems)
        {
            if ((bool)(object)oldItems)
            {
                for (int i = 0; i < oldItems.Length; i++)
                {
                    ToggleButton tb = (ToggleButton)oldItems[i];
                    tb.remove_pushed(m_onItemPushedHandler);
                    tb.remove_unpushed(m_onItemUnPushedHandler);
                }
            }
            if ((bool)(object)newItems)
            {
                for (int i = 0; i < newItems.Length; i++)
                {
                    ToggleButton tb = (ToggleButton)newItems[i];
                    tb.add_pushed(m_onItemPushedHandler);
                    tb.add_unpushed(m_onItemUnPushedHandler);
                }
            }
        }
    }
}